<?php
class Common
{
	public static $user_first_spoken_language_tag;
	public static $user_first_written_language_tag;
	public static $using_template_name;
	
	private function __construct()
	{
		
	}
	
	public static function firstToUpper($s)
	{
		$first = substr ( $s, 0, 1 );
		$left = substr ( $s, 1 );
		return strtoupper ( $first ) .$left;
	}
	
	public static function firstToLower($s)
	{
		$first = substr ( $s, 0, 1 );
		$left = substr ( $s, 1 );
		return strtolower ( $first ) .$left;
	}
	
	public static function decodeUnicode($str)
	{
	return preg_replace_callback('/\\\\u([0-9a-f]{4})/i',
		create_function(
			'$matches',
			'return mb_convert_encoding(pack("H*", $matches[1]), "UTF-8", "UCS-2BE");'
		),
		$str);
	}
	
	public static function getUsingTempleteName()
	{
		if(!empty(self::$using_template_name))
		{
			return self::$using_template_name;
		}
		$templete_name = null;
		if(isset($_GET['t']))
		{
			$templete_name = empty($_GET['t']);
		}
		else
		{
			if(isset($_COOKIE['t']))
			{
				$templete_name = $_COOKIE['t'];
			}
		}
		if(!empty($templete_name))
		{
			$templete_name_low = strtolower($templete_name);
			$templetes_name = ConfigParser::get('templete','templetes_name');
			if(!isset($templetes_name[$templete_name_low]))
			{
				$t = ConfigParser::get('templete','default_templete_name');
			}
			else
			{
				$t = $templete_name_low;
			}
		}
		else
		{
			$t = ConfigParser::get('templete','default_templete_name');
		}
		setcookie('t', $t, time()+ConfigParser::get('cookie','ttl'),'/');
		self::$using_template_name = $t;
		return $t;
	}
	
	public static function getUsingTempletePath()
	{
		return ConfigParser::get('path','templates_path') . self::getUsingTempleteName().'/';
	}
	
	public static function getDbEngineClassName($db_engine)
	{
		return self::underlineToClassName($db_engine . 'Engine');
	}
	
	public static function getUserFirstWrittenLanguageTag()
	{
		if(!empty(self::$user_first_written_language_tag))
		{
			return self::$user_first_written_language_tag;
		}
		$first = null;
		if(isset($_GET['wl'])||isset($_GET['hl']))
		{
			$first = empty($_GET['wl']) ? $_GET['hl']:$_GET['wl'];
		}
		else
		{
			if(isset($_COOKIE['wl'])&&!empty($_COOKIE['wl']))
			{
				$first = $_COOKIE['wl'];
			}
			else
			{
				//when user delete all languages in browser settings，there is no $_SERVER['HTTP_ACCEPT_LANGUAGE']
				if(isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])&&!empty($_SERVER['HTTP_ACCEPT_LANGUAGE']))
				{
					$lang = $_SERVER['HTTP_ACCEPT_LANGUAGE'];
					$pos = stripos($lang,",");
					if($pos!==false)
					{
						$first = substr($lang,0,$pos);
					}
					else
					{
						$first = $lang;
					}
				}
			}
		}
		if(!empty($first))
		{
			$first_low = strtolower($first);
			$written_language_tags_map = ConfigParser::get('language','written_languages_tag_map');
			if(!isset($written_language_tags_map[$first_low]))
			{
				$wl = ConfigParser::get('language','default_written_language_tag');
			}
			else
			{
				$wl = $written_language_tags_map[$first_low];
			}
		}
		else
		{
			$wl = ConfigParser::get('language','default_written_language_tag');
		}
		setcookie("wl", $wl, time()+ConfigParser::get('cookie','ttl'),'/');
		self::$user_first_written_language_tag = $wl;
		return $wl;
	}

	public static function getWrittenLanguagePack()
	{
		$written_language_tag = self::getUserFirstWrittenLanguageTag();
		require(dirname(__FILE__).'/../view/written_language_pack/'.$written_language_tag.'/common.php');
		return $wlp;
	}
	
	public static function isUserAgentSupportXhtml()
	{
		if(isset($_SERVER['HTTP_ACCEPT'])&&(stripos($_SERVER['HTTP_ACCEPT'],'application/xhtml+xml')!=-1))
		{
			return true;
		}
		return false;
	}
	
	public static function output($v=array(),$content_type='',$dtd='')
	{
		if(empty($content_type))
		{
			$content_type = ConfigParser::get('templete', 'default_page_content_type');
		}
		if(empty($dtd))
		{
			$dtd = ConfigParser::get('templete', 'default_page_dtd');
		}
		$combination_path = self::getUsingTempletePath().'combination/'.$combination.'.tpl';
		switch ($content_type)
		{
			case 'auto_xhtml_html':
				$v['content_type'] = 'html';
				if(self::isUserAgentSupportXhtml()===true)
				{
					$v['content_type'] = 'xhtml';
					header('Content-Type: application/xhtml+xml');
				}
				else
				{
					header('Content-Type: text/html');
				}
				$v['dtd'] = $dtd;
				$v['wlp'] = self::getWrittenLanguagePack();
				require $combination_path;
				break;
			case 'xhtml'://warning: if you enforce use xhtml,IE876 can't see.
				$v['content_type'] = 'xhtml';
				header('Content-Type: application/xhtml+xml');
				$v['dtd'] = $dtd;
				$v['wlp'] = self::getWrittenLanguagePack();
				require $combination_path;
				break;
			case 'html':
				$v['content_type'] = 'html';
				header('Content-Type: text/html');
				$v['dtd'] = $dtd;
				$v['wlp'] = self::getWrittenLanguagePack();
				require $combination_path;
				break;
			case 'json':
				header('Content-Type: application/json');
				echo json_encode($v);
				break;
		}
	}
	
	public static function underlineToHump($s)
	{
		$tmp = explode('_',$s);
		$result = $tmp[0];
		unset($tmp[0]);
		foreach($tmp as $value)
		{
			$result .= self::firstToUpper($value);
		}
		return $result;
	}
	
	public static function underlineToClassName($s)
	{
		$tmp = explode('_',$s);
		$result = '';
		foreach($tmp as $value)
		{
			$result .= self::firstToUpper($value);
		}
		return $result;
	}
}
?>